<?
include 'includes/specs_functions.php';
echo printGoogleAnalytics();
#------------------------------------------------------------------------
# Connect to PADS
$DB = getDB();
#-------------------------------$Session information
$SESSION = get_session($DB);
$userCICUID = $SESSION->Character;
$THEME = $SESSION->Theme;
$userAVATAR = $SESSION->GetCharacter('avatar');
$padCICUID = $_POST['CICUID'];
$padAVATAR = $_POST['AVATAR'];
#---------------------------------------------------
//Show($SESSION->GetCharacter('id'));
//Show($SESSION->GetCharacter('avatar'));

$sId = $_POST['specId'];
$amount = $_POST['amount'];

//$padCICUID = 9545338;  // Nemi Chan

if ($userCICUID!=""){
	$canWrite = true;
}else{
	$canWrite = false;
}

if (isset($_POST['specId']) && isset($_POST['amount'])){

	$output = "
				<script type='text/javascript'>
					$(function(){
						$('.ratings').children().not(':radio').hide();
						$('.ratings').stars({
							disabled: true
						});
					});
					
					$(document).ready(function(){
						$('.delete-comment').click(function(e) {
							var clicked = jQuery(e.target);
							var id = clicked.attr('id')
							deleteComment(id, '".$sId."');
						});
						$('.more-comment').click(function(e) {
							loadSpecComments(".$sId.",".($amount+5).");
						});
					});
				</script>
				<table id='me_spec_comment' class='content-subtitle-table'>";
				
					
				$abfrage = "SELECT * FROM (
					   SELECT user.".TBL_CHARACTERS_CICUID." as uId
					   ,comm.".SPEC_TBL_COMMENTS_ID." as id
					   ,comm.".SPEC_TBL_COMMENTS_SPEC_ID." as specId
					   ,comm.".SPEC_TBL_COMMENTS_COMMENT." as comment
					   ,DATE_FORMAT(comm.".SPEC_TBL_COMMENTS_UDATE.",'%b %d %Y %H:%i:%S') as c_udate
					   ,spec.".SPEC_TBL_SPECS_PUBLIC." as public
					   ,user.".TBL_CHARACTERS_NAME." as Name
					   ,user.".TBL_CHARACTERS_AVATAR." as Avatar
					   ,'' as value
					   ,'COMM' as type 
				FROM ((
				".SPEC_TBL_COMMENTS." as comm 
				LEFT JOIN 
				".SPEC_TBL_SPECS." as spec
				ON comm.".SPEC_TBL_COMMENTS_SPEC_ID." = spec.".SPEC_TBL_SPECS_ID.")
				INNER JOIN ".TBL_CHARACTERS." as user
				ON user.".TBL_CHARACTERS_CICUID." = comm.".SPEC_TBL_COMMENTS_USER_ID.")		
				
				UNION 
				
				SELECT user.".TBL_CHARACTERS_CICUID." as uId 
					,rate.".SPEC_TBL_RATINGS_ID." as id
					,rate.".SPEC_TBL_RATINGS_SPEC_ID." as specId
					,(CASE WHEN rate.".SPEC_TBL_RATINGS_CDATE."= rate.".SPEC_TBL_RATINGS_UDATE." THEN 'rated this spec with' ELSE 'updated his rating to' END) as comment
					,DATE_FORMAT(rate.".SPEC_TBL_RATINGS_CDATE.",'%b %d %Y %H:%i:%S') as c_udate
					,spec.".SPEC_TBL_SPECS_PUBLIC." as public
					,user.".TBL_CHARACTERS_NAME." as Name
					,user.".TBL_CHARACTERS_AVATAR." as Avatar
					,rate.".SPEC_TBL_RATINGS_VALUE." as value
					,'RATE' as type
					
				FROM ((".SPEC_TBL_RATINGS." as rate
				LEFT JOIN 
				".SPEC_TBL_SPECS." as spec
				ON rate.".SPEC_TBL_RATINGS_SPEC_ID." = spec.".SPEC_TBL_SPECS_ID.")
				INNER JOIN ".TBL_CHARACTERS." as user
				ON user.".TBL_CHARACTERS_CICUID." = rate.".SPEC_TBL_RATINGS_USER_ID.")
				
				) as A";
				
				if(!$canWrite){
					$abfrage .= " WHERE A.specId = \"".$sId."\" AND A.public = 'Y'";
				}else{
					$abfrage .= " WHERE A.specId = \"".$sId."\"";
				}
				$abfrage .= " ORDER BY A.c_udate DESC";

				$result = $DB->RawQuery($abfrage);

				$num = $result[0];	
				$i = 1;
								
				$spec_table = "(
							".SPEC_TBL_SPECS." as spec
							LEFT JOIN
							".TBL_CHARACTERS." as user
							ON spec.".SPEC_TBL_SPECS_USER_ID." = user.".TBL_CHARACTERS_CICUID."
							) ";
				$spec_col = array("user.".TBL_CHARACTERS_CICUID." as CICUID"
									,"spec.".SPEC_TBL_SPECS_DESCRIPTION." as description"
									, "user.".TBL_CHARACTERS_NAME." as Name"
									, "spec.".SPEC_TBL_SPECS_CDATE." as cdate"
									, "DATE_FORMAT(spec.".SPEC_TBL_SPECS_UDATE.",'%b %d %Y %H:%i:%S') as udate"
									, "user.".TBL_CHARACTERS_AVATAR." as Avatar" );
				$spec_where = array("spec.".SPEC_TBL_SPECS_ID." = \"".$sId."\"");
				$spec_orderBy = "spec.".SPEC_TBL_SPECS_DESCRIPTION;
				$spec_direction = "ASC";
				
				$spec_result = $DB->GetData($spec_table ,$spec_col ,true ,$spec_where, $spec_orderBy, $spec_direction);			
				
				$spec_num = $spec_result[0];
				foreach ($spec_result[3] as $row) {

					$desc = $row["description"];
					$av = $row["Avatar"];
					$usr = $row["Name"];
					$crea = $row["cdate"];
					$up = $row["udate"];
					$ownerId = $row['CICUID'];
				}
				

				$specAvatar = "<div class='border-av'><img class='avatar-pic' style='width:36px' src='".$av."?".time()."'/></div>";

				$output .= "
					<tr class='spec-row tr-table border-bottom'>
						<td class='td-table ver-top cur-point' style='padding: 10px;'>
							<a class='cur-point'  name='no' href='?".$ownerId."'>".$specAvatar."</a>
						</td>
						<td style='padding-top: 10px;'>
							<div>
								<a class='cur-point fl-left bold-text' style='font-size: 11px;' name='no' href='?".$ownerId."'>".$usr."</a>";
				if($crea == $up){
					$output .= "<div class='normal-text bold-text fl-left' style='margin-left: 4px;	'>created this spec";
				}else{
					$output .= "<div class='normal-text bold-text fl-left' style='margin-left: 4px;'>updated his spec";
				}
								
				if($desc != ""){
					$output .= " describing it as:</div>
								<div class='spec-comment normal-text fl-left intro' style='overflow: hidden;white-space: pre-wrap'>".$desc."</div>";
					
				}else{
					$output .= "</div>";
				}
				$output .= "</div>
							<div class='normal-text font-10 fl-left'>
								<div class='op-4 fl-left' style='padding-bottom:10px; padding-right:5px;padding-top: 5px;'>".$up."</div>
							</div>
							</div>
						</td>
					</tr>";
				
				if($canWrite){
					$output .= "
					<tr class='spec-row tr-table border-bottom spec-row-static'>
						<td class='td-table ver-top cur-point' style='padding: 10px;'>
							<a class='cur-point' name='no' href='?".$userCICUID."'><div class='border-av'><img class='avatar-pic' style='width:36px' src='".$userAVATAR."?".time()."'/></div></a>
						</td>
						<td id='spec-comment-write-container' class='td-table ver-top' style='padding-right: 22px; padding-left: 5px; padding-top: 7px;'>
							<textarea id='write-comment' type='text'>Comment on ".$usr."'s spec...</textarea>
						</td>
					</tr>
					<script>
						var shiftPressed = false;
						$(window).keydown(function(evt) {
						  if (evt.which == 16) { // shift
							shiftPressed = true;
						  }
						}).keyup(function(evt) {
						  if (evt.which == 16) { // shift
							shiftPressed = false;
						  }
						});
						$('#write-comment').keypress(function(event) {
							if (event.which == 13 && !(shiftPressed)){
								writeComment(".$sId.", $(this).val())
								event.preventDefault();
							}
						});

						$('#write-comment').focus(function(){
							$(this).val('');
						});
					</script>";
				}else{
					$output .= "
					<tr class='spec-row tr-table border-bottom spec-detail-list-selected'>
						<td class='td-table ver-top cur-point' style='padding: 10px;'>
							<a class='' name='no' ><div class='border-av'><img src='specs/media/no-av.png' style='width:36px;height:36px;'/></div></a>
						</td>
						<td id='spec-comment-write-container' class='td-table ver-top' style='padding-right: 22px; padding-left: 5px; padding-top: 7px;'>
							<textarea id='write-comment' readonly type='text'>Login to write a comment...</textarea>
						</td>
					</tr>";
				}
				
				if($num>0){
					foreach ($result[2] as $row) {
						$MyAvatar = "<div class='border-av'><img class='avatar-pic' style='width:36px;' src='".$row['Avatar']."?".time()."'/></div>";

						$output .= "<tr id='tr_".$row['comId']."' class='spec-row tr-table ";
						if($i != $amount && $i != $num){
							$output .= "border-bottom";
						}
						$output .= "' name='".$row['comId']."'>
							<td class='td-table ver-top cur-point' style='padding: 10px;'>
								<a class='cur-point' name='no' href='?".$row['uId']."'>".$MyAvatar."</a>
							</td>
							<td class='td-table ver-top' style='padding-top: 10px;'>
								<div >
									<a class='cur-point fl-left bold-text' name='no' style='font-size:11px' href='?".$row['uId']."'>".$row['Name']."</a>";
						
						if($row['type'] == "RATE"){
							$output .= "
								<div class='normal-text bold-text fl-left' style='overflow: hidden;margin-left: 4px;'>".$row['comment']."</div>
								<div class='fl-left' style='padding-left:5px; padding-top: 1px; width: 80px;'> 
									<form class='ratings' action='' method='post'>";
											
							foreach (get_options() as $id => $title){
								$output .= 	"<input type='radio' name='rate' value='".$id."' id='rate".$id."'";
								if($id==$row['value']){
									$output .= 	" checked";
								}
								$output .= 	" />";
							}
							
							$output .= "	</form>
								</div><div style='clear:both;'/>";
						}else{
						
							$output .= "
									</div>
									<div class='spec-comment normal-text intro' style='overflow: hidden;white-space: pre-wrap;'>".$row['comment']."</div>";
						}
								
						$output .= "	<div class='normal-text font-10 fl-left'>
									<div class='op-4 fl-left' style='padding-bottom:5px; padding-right:5px;padding-top: 5px;'>".$row['c_udate']."</div>
								";
									
						
						if($userCICUID == $row['uId'] && $row['type'] != "RATE"){
							$output .= 	"<span id='split' style='float: left;margin-top: 7px;'></span>
									<a id='".$row['id']."' class='delete-comment blue-text fl-left pad-lr-5 font-10' style='cursor: pointer;margin-left: 5px;margin-top: 5px;' name='no' >Delete</a>";
						}
						
						$output .= "</div>
								</div>
							</td>
						</tr>";
						if($i == $amount){
							break;
						}
						$i++;
					}
				}
				
				if($i<$num){
					// MORE COMMENTS TO SHOW
					$output .= "<tr>
						<td  colspan='2' style='text-align: center;padding-top: 10px;'>
							<div class='more-comment center cur-point arrow-down' style='display:inline;'/>
						</td>
					</tr>";
					
				}
			
		$output .= "</table><div style='clear:both;'>";

	echo  $output;
}

#------------------------------------------------------------------------
# Disconnect
leaveDB($DB);

?>